import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Test {

    static class Student {
        String name;
        int age;

        public Student(int age) {
            this.age = age;
        }
    }

    //按照年龄比，小根堆，从小到大
    static class IntSmall implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age - o2.age;
        }
    }

    //按照年龄比，大根堆，从大到小
    static class IntBig implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o2.age - o1.age;
        }
    }

    public static void main(String[] args) {
        PriorityQueue<Student> heap = new PriorityQueue<>(new IntSmall());
        heap.offer(new Student(12));
        heap.offer(new Student(33));
        heap.offer(new Student(24));
        heap.offer(new Student(10));
        heap.offer(new Student(6));
        System.out.println(heap.peek().age);

        PriorityQueue<Student> heap2 = new PriorityQueue<>(new IntBig());
        heap2.offer(new Student(12));
        heap2.offer(new Student(33));
        heap2.offer(new Student(24));
        heap2.offer(new Student(10));
        heap2.offer(new Student(6));
        System.out.println(heap2.peek().age);
    }


    public static void main3(String[] args) {
        int[] arrays = {12,34,55,2,5,66,18,39,77};
        int[] ret = findMax(arrays,3);
        System.out.println(Arrays.toString(ret));
    }
    //找出数组中最大的k个数。以任意顺序返回这k个数均可
    public static int[] findMax(int[] arrays,int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            minHeap.offer(arrays[i]);
        }
        for (int i = k; i < arrays.length; i++) {
            if(minHeap.peek() < arrays[i]) {
                minHeap.poll();
                minHeap.offer(arrays[i]);
            }
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    public static void main2(String[] args) {
        //找出数组中最小的k个数。以任意顺序返回这k个数均可
        //时间复杂度
        int[] arrays = {12,34,55,2,5,66,18,39,77};
        int[] ret = find(arrays, 3);
        System.out.println(Arrays.toString(ret));//2 5 12
    }
    public static int[] find(int[] array,int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for (int i = 0; i < array.length; i++) {
            minHeap.offer(array[i]);
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }
    public static void main1(String[] args) {
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        heap.offer(3);
        heap.offer(6);
        heap.offer(9);
        System.out.println(heap.peek());
        //peek 值为3，说明这是建立的小根堆
    }
}
